﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entidades.Espectaculos;
using Negocio.Espectaculos;

public partial class Espectaculos_Configuracion_frmRepresentante : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            cargarRepresentante();
        } 
    }

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {

            cargarRepresentante();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmRegistroRepresentante.aspx");
    }

    private void cargarRepresentante()
    {

        int estado = -1;
        int.TryParse(ddlEstado.SelectedValue, out estado);

        string razonSocial = txtRazonSocial.Text.Trim();
        string ruc = txtRUC.Text.Trim();

        List<EspRepresentante> lista = EspRepresentanteBLL.ListarRepresentante(estado, -1, razonSocial, ruc);

        grvRepresentante.DataSource = lista;
        grvRepresentante.DataBind();

    }

    protected void grvRepresentante_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null)
        {
            ImageButton ibtnEditar = (ImageButton)e.Row.FindControl("ibtnEditar");
            ibtnEditar.CommandArgument = e.Row.RowIndex.ToString();

            Image imgEstado = (Image)e.Row.FindControl("imgEstado");
            string estado = DataBinder.GetPropertyValue(e.Row.DataItem, "Estado").ToString();
            if (estado == "1")
            {
                imgEstado.ImageUrl = "~/Images/botones/activo.png";
            }
            else
            {
                imgEstado.ImageUrl = "~/Images/botones/inactivo.png";
            }
        }
    }

    protected void ibtnEditar_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtnEditar = (ImageButton)sender;
        int index = Convert.ToInt32(ibtnEditar.CommandArgument);
        string id = grvRepresentante.DataKeys[index].Values["IdRepresentante"].ToString();

        Response.Redirect("frmRegistroRepresentante.aspx?id=" + id + "&ref=edit");

    }

}